<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">灵堂名称</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.LinnameLike" placeholder="请输入灵堂名称" @keydown.enter.native="btnSearch"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">逝者姓名</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.RegnameLike" placeholder="请输入逝者姓名"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">开始日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.LihbegdateSta" type="date" placeholder="请选择日期">
          </el-date-picker>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">结束日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.LihbegdateEnd" type="date" placeholder="请选择日期">
          </el-date-picker>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)" :key="index">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <!--操作弹窗  -->
    <main-model ref="dialog" @refreshData="getData"></main-model>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
  .el-form-item {
    margin-bottom: 0px;
  }
  .btnCenter {
    text-align: center;
  }
</style>
<script>
  import sweetModal from "@/components/sweetModal";
  import apiBase from "@/api/apiBase.js";
  import dataTable from "@/components/Table/DataTable";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  import mainModel from './hallUseMain';
  import { ORDER } from "@/const/orderConst.js";
  const ADD = "新增";
  const MODIFY = "修改";
  const DELETE = "删除";
  const INFO = "详情";
  const BEG = "开始使用";
  const END = "结束使用";
  const DAOCHU = '导出';
  const REG = '灵堂登记';
  export default {
    name: "linUse",
    mixins: [manage],
    data() {
      return {
        selectData: {},
        ischangered: null,
        ischangnb: null,
      };
    },
    methods: {
      //获取表头
      getlinColumn() {
        apiBase.DoService({},
          apiBase.getApiAdress().Column.GetMourningUseColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      //获取查询数据
      getData() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.LihbegdateSta = formatDate(Data.LihbegdateSta, "yyyyMMdd");
        Data.LihbegdateEnd = formatDate(Data.LihbegdateEnd, "yyyyMMdd");
        var data = {
          PageIndex: this.pageOrder.currentPage,
          PageSize: this.pageOrder.pageSize,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(data, apiBase.getApiAdress().MourningUse.Query, res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
          this.$nextTick(function() {
              let rows = document.getElementsByClassName("el-table__row");
              if(this.ischangered){
                this.dataList.forEach((item,index) => {
                    rows[index].style.color = '#606266';
                    if(item.REGCRETYPE === 'A'){
                        rows[index].style.color = 'red';
                    }
                });
              };
              if(this.ischangnb){
                this.dataList.forEach((item,index) => {
                    rows[index].style.color = '#606266';
                    if(item.REGCRETYPE === 'A'||item.REGCRETYPE === 'C'){
                        rows[index].style.color = '#007eff';
                    }
                    if(item.REGCRETYPE === 'D'){
                        rows[index].style.color = 'red';
                    }
                });
              };
          });
        });
      },
      //功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          case ADD:
          case REG:
          case BEG:
            this.$refs.dialog.open(this.order);
            break;
          case MODIFY:
          case INFO:
            this.$refs.dialog.open(this.order, this.currentRow.LIHID);
            break;
          case END:
            if (this.currentRow.LIHENDDATE != '') {
              this.$message({
                message: "该条记录已结束使用",
                type: "error"
              })
            } else {
              this.$refs.dialog.open(this.order, this.currentRow.LIHID);
            }
            break;
          case DELETE:
            this.dataDelete();
            break;
          case DAOCHU:
            this.exportExcel();
          default:
        }
      },
      //导出数据
      exportExcel(){
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.LihbegdateSta = formatDate(Data.LihbegdateSta, "yyyyMMdd");
        Data.LihbegdateEnd = formatDate(Data.LihbegdateEnd, "yyyyMMdd");
        var data = {
          PageIndex: -1,
          PageSize: this.pageOrder.pageSize,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(data, apiBase.getApiAdress().MourningUse.Query, res => {
          let OutData = res.data.content.OutData;
          var startTime;
          var endTime;
          if(this.selectData.LihbegdateSta){
            startTime = this.selectData.LihbegdateSta.toLocaleDateString();
          }else{
            startTime = '空';
          }
          if(this.selectData.LihbegdateEnd){
            endTime = this.selectData.LihbegdateEnd.toLocaleDateString();
          }else{
            endTime = '空';
          }
          var header = '守灵室使用' + '(' + startTime + '--' + endTime + ')';
          if (this.dataList.length === 0){
              this.$message.error("无导出数据！");
              return;
          }
          this.downloadLoading = true;
          require.ensure([], () => {
            const { export_json_to_excel } = require("@/vendor/Export2Excel");
            export_json_to_excel(this.headList, OutData, header);
            this.downloadLoading = false;
          });

          
        });
      },
       // 控制是否显示
      btnShow(val){
       if(val == BEG || val == ADD || val == DAOCHU|| val == REG) return true;
       if(this.currentRow){
         if(val == MODIFY || val == DELETE || val == INFO || val == END) return true;
       }
      },
      //使用删除
      dataDelete() {
        var data = {
          LIHID: this.currentRow.LIHID
        };
        this.$confirm("此操作将永久删除该使用信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(callback => {
          apiBase.DoService(
            data,
            apiBase.getApiAdress().MourningUse.Delete,
            res => {
              this.getData();
              this.$message({
                message: "删除成功！",
                type: "success"
              });
            }
          );
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      },
      //查询条件重置
      btnRepeat() {
        this.selectData = {};
        this.getData();
      },
    },
    //路由的时候刷新组件
    activated: function() {
      this.getData();
    },
    components: {
      mainModel,
      dataTable
    },
    created() {
      apiBase.DoService({},apiBase.getApiAdress().Param.GetIsDataTableReddening,res => {
        this.ischangered = res.data.content; 
      });
      apiBase.DoService({},apiBase.getApiAdress().Param.GetIsCreChangeRed,res => {
        this.ischangnb = res.data.content; 
      });
      this.getlinColumn();
      this.getData();
    }
  };
</script>